/** The inventory window implementation.
 */
function InventoryMode()
{
	this.selectedIndex = 0;
	this.height = 28;
	this.width = 37;
	this.top = Math.floor((host.screen.height - this.height) / 2);
	this.left = Math.floor((host.screen.width - this.width) / 2);
	this.render();
}
InventoryMode.prototype =
{
	/** Label data.
	 */
	_labelData:
	[
		{label: "Equipment", line: 0},
		{label: "", line: 6},
		{label: "Possessions", line: 7}
	],
	/** Selection line data.
	 */
	_selectionData:
	[
		{type: "equipment", key: "hands", line: 1},
		{type: "equipment", key: "head", line: 2},
		{type: "equipment", key: "body", line: 3},
		{type: "equipment", key: "legs", line: 4},
		{type: "equipment", key: "feet", line: 5},
		{type: "inventory", index: 0, line: 8},
		{type: "inventory", index: 1, line: 9},
		{type: "inventory", index: 2, line: 10},
		{type: "inventory", index: 3, line: 11},
		{type: "inventory", index: 4, line: 12},
		{type: "inventory", index: 5, line: 13},
		{type: "inventory", index: 6, line: 14},
		{type: "inventory", index: 7, line: 15},
		{type: "inventory", index: 8, line: 16},
		{type: "inventory", index: 9, line: 17},
		{type: "inventory", index: 10, line: 18},
		{type: "inventory", index: 11, line: 19},
		{type: "inventory", index: 12, line: 20},
		{type: "inventory", index: 13, line: 21},
		{type: "inventory", index: 14, line: 22},
		{type: "inventory", index: 15, line: 23},
		{type: "inventory", index: 16, line: 24},
		{type: "inventory", index: 17, line: 25},
		{type: "inventory", index: 18, line: 26},
		{type: "inventory", index: 19, line: 27}
	],
	/** Handle a user action.
	 *@param action The action to handle.
	 */
	handleAction: function(action)
	{
		var lastSelection = this.selectedIndex;
		switch(action)
		{
			case "up": --this.selectedIndex; break;
			case "down": ++this.selectedIndex; break;
			case "left": this.selectedIndex -= 5; break;
			case "right": this.selectedIndex += 5; break;
			case "cancel":
				removeMode(this);
				return true;
				break;
		}
		if(this.selectedIndex < 0)
			this.selectedIndex = 0;
		if(this.selectedIndex >= this._selectionData.length)
			this.selectedIndex = this._selectionData.length - 1;
		if(this.selectedIndex != lastSelection)
		{
			this._renderSelection(this._selectionData[this.selectedIndex], true);
			this._renderSelection(this._selectionData[lastSelection], false);
		}
		return true;
	},
	/** Render a selection line.
	 *@param selection The _selectionData member to render.
	 *@param selected Is this selection currently selected?
	 */
	_renderSelection: function(selection, selected)
	{
		host.screen.blitRect(
			this.left,
			this.top + selection.line,
			this.width,
			1,
			" ",
			null,
			selected ? Color.darkBlue : Color.black
		);
		var leftOfs = 4;
		if(selection.type == "inventory")
			leftOfs += 3;
		var string = "";
		if(selection.type == "equipment")
		{
			switch(selection.key)
			{
				case "hands": string = "Hands: "; break;
				case "head": string = "Head:  "; break;
				case "body": string = "Body:  "; break;
				case "legs": string = "Legs:  "; break;
				case "feet": string = "Feet:  "; break;
			}
			var name = player.equipment[selection.key];
			if(!name)
				name = "none";
			else
				name = name.name;
			string += name;
		}
		else if(selection.type == "inventory")
		{
			string = player.inventory[selection.index];
			if(!string)
				string = "";
			else
				string = string.name;
		}
		host.screen.blitString(
			this.left + leftOfs,
			this.top + selection.line,
			"%fg" + string
		);
	},
	/** Render the inventory screen.
	 */
	render: function()
	{
		var i;

		// Draw the box
		host.screen.drawMenuBox(this.left, this.top, this.width, this.height);

		// Draw labels
		for(i = 0; i < this._labelData.length; ++i)
		{
			var label = this._labelData[i];
			host.screen.blitRect(
				this.left,
				this.top + label.line,
				this.width,
				1,
				" ",
				null,
				Color.black
			);
			host.screen.blitString(
				this.left + (Math.floor((this.width - label.label.length) / 2)),
				this.top + label.line,
				"%d0" + label.label
			);
		}
		for(i = 0; i < this._selectionData.length; ++i)
		{
			this._renderSelection(this._selectionData[i], i == this.selectedIndex);
		}
	}
};
